package com.android.phoenix.fragments.offlinemap;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.android.phoenix.R;
import com.android.phoenix.adapters.MyOfflineMapCityListAdapter;
import com.android.phoenix.adapters.MyOfflineMapCityListAdapter.ViewHolder;
import com.android.phoenix.adapters.OfflineMapExpListAdapter;
import com.android.phoenix.adapters.OfflineMapExpListAdapter.ChildHolder;

public class OfflineMapAllFragment extends Fragment implements
		OnChildClickListener, OnItemClickListener, OnGroupExpandListener,
		OnGroupCollapseListener {
	private final static String[] testCitys = new String[] { "武汉", "北京", "上海",
			"广州", "深圳" };
	private final static String[] testProvince = new String[] { "湖北", "湖南",
			"广东", "江西" };

	private ListView lvHotCity = null;
	private ExpandableListView explvProvince = null;
	private MyOfflineMapCityListAdapter mHotAdapter = null;
	private OfflineMapExpListAdapter mExpAdapter = null;

	private int expListHeight = 0;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fra_offlinemap_all, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		initViews();
	}

	private void initViews() {
		lvHotCity = (ListView) getActivity().findViewById(R.id.lvHotCity);
		explvProvince = (ExpandableListView) getActivity().findViewById(
				R.id.explvProvince);

		mHotAdapter = new MyOfflineMapCityListAdapter(getActivity(), testCitys);
		lvHotCity.setAdapter(mHotAdapter);
		lvHotCity.setOnItemClickListener(this);
		setHotCityListHeight(lvHotCity);
		mExpAdapter = new OfflineMapExpListAdapter(getActivity(), testProvince,
				testCitys);
		explvProvince.setAdapter(mExpAdapter);
		explvProvince.setOnChildClickListener(this);
		explvProvince.setOnGroupExpandListener(this);
		explvProvince.setOnGroupCollapseListener(this);
		setAllCityExpListHeight(explvProvince);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		if (R.id.lvHotCity == arg0.getId()) {
			ViewHolder mHolder = (ViewHolder) arg1.getTag();
			mHolder.tvInstall.setVisibility(View.VISIBLE);
			mHolder.imgDownload.setVisibility(View.GONE);
		}
	}

	@Override
	public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2,
			int arg3, long arg4) {
		// TODO Auto-generated method stub
		if (R.id.explvProvince == arg0.getId()) {
			ChildHolder childHolder = (ChildHolder) arg1.getTag();
			childHolder.tvInstall.setVisibility(View.VISIBLE);
			childHolder.imgDownload.setVisibility(View.GONE);
		}
		return false;
	}

	@Override
	public void onGroupCollapse(int arg0) {
		// TODO Auto-generated method stub
		Log.i("wanchao", "onGroupCollapse--1");
		if (explvProvince == null) {
			return;
		}
		Log.i("wanchao", "onGroupCollapse--2");
		ExpandableListAdapter expListAdapter = explvProvince
				.getExpandableListAdapter();
		if (expListAdapter == null) {
			return;
		}
		Log.i("wanchao", "onGroupCollapse--3");

		for (int i = 0; i < expListAdapter.getChildrenCount(arg0); i++) {
			View listChildItem = expListAdapter.getChildView(arg0, i, false,
					null, explvProvince);
			listChildItem.measure(0, 0);
			expListHeight -= listChildItem.getMeasuredHeight();
		}
		expListHeight -= (explvProvince.getDividerHeight() * expListAdapter
				.getChildrenCount(arg0));
		ViewGroup.LayoutParams params = explvProvince.getLayoutParams();
		params.height = expListHeight;
		explvProvince.setLayoutParams(params);
		Log.i("wanchao", "expListHeight_close = " + expListHeight);
	}

	@Override
	public void onGroupExpand(int arg0) {
		// TODO Auto-generated method stub
		Log.i("wanchao", "onGroupExpand--1");
		if (explvProvince == null) {
			return;
		}
		Log.i("wanchao", "onGroupExpand--2");
		ExpandableListAdapter expListAdapter = explvProvince
				.getExpandableListAdapter();
		if (expListAdapter == null) {
			return;
		}
		Log.i("wanchao", "onGroupExpand--3");

		for (int i = 0; i < expListAdapter.getChildrenCount(arg0); i++) {
			View listChildItem = expListAdapter.getChildView(arg0, i, false,
					null, explvProvince);
			listChildItem.measure(0, 0);
			expListHeight += listChildItem.getMeasuredHeight();
		}
		expListHeight += (explvProvince.getDividerHeight() * expListAdapter
				.getChildrenCount(arg0));
		ViewGroup.LayoutParams params = explvProvince.getLayoutParams();
		params.height = expListHeight;
		explvProvince.setLayoutParams(params);
		Log.i("wanchao", "expListHeight_open = " + expListHeight);
	}

	public void setHotCityListHeight(ListView listView) {
		if (listView == null) {
			return;
		}
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}

	public void setAllCityExpListHeight(ExpandableListView expListView) {
		if (expListView == null) {
			return;
		}
		ExpandableListAdapter expListAdapter = expListView
				.getExpandableListAdapter();
		if (expListAdapter == null) {
			return;
		}
		expListHeight = 0;
		for (int i = 0; i < expListAdapter.getGroupCount(); i++) {
			View listGroupItem = expListAdapter.getGroupView(i, true, null,
					expListView);
			listGroupItem.measure(0, 0);
			expListHeight += listGroupItem.getMeasuredHeight();
		}
		expListHeight += (expListView.getDividerHeight() * (expListAdapter
				.getGroupCount() - 1));
		ViewGroup.LayoutParams params = expListView.getLayoutParams();
		params.height = expListHeight;
		expListView.setLayoutParams(params);
		Log.i("wanchao", "expListHeight_init = " + expListHeight);
	}
}
